package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_BILL_TOTAL_ERROR;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.BillService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.Bill;
import org.apache.dolphinscheduler.dao.entity.User;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;

/**
 * <p>
 * 收支明细表 前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-08-14
 */
@Api(tags = "收支明细 API")
@RestController
@RequestMapping("/bill")
public class BillController extends BaseController {

    @Autowired
    private BillService billService;

    /**
     * 获取收支明细列表
     */
    @GetMapping()
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取收支明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billType", value = "收支类型 0 = 支出 1 = 收入"),
            @ApiImplicitParam(name = "tradeType", value = "交易类型 0 = 消费 1 = 平台服务"),
    })
    @PermissionCheck(permission = "operation:bill:list")
    public Result list(
                       @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestParam(value = "userId", required = false) Integer userId,
                       @RequestParam(value = "billType", required = false) Integer billType,
                       @RequestParam(value = "tradeType", required = false) Integer tradeType,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNo") Integer pageNo) {
        Result result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        Bill bill = new Bill();
        bill.setBillType(billType);
        bill.setTradeType(tradeType);
        bill.setUserId(userId);
        return billService.selectPageBillList(loginUser, bill, pageSize, pageNo, startDate, endDate);
    }

    /**
     * 获取账户总收入支出
     */
    @GetMapping("/total")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("获取账户总收入支出")
    @PermissionCheck(permission = "operation:overview:list")
    public Result total(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        return billService.selectBillTotal(loginUser);
    }

    /**
     * 年收入支出趋势
     */
    @GetMapping("/chart/year")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("年收入支出趋势")
    @PermissionCheck(permission = "operation:overview:list")
    public Result chartBillInYear(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                  @RequestParam(value = "startDate", required = false) String startDate,
                                  @RequestParam(value = "endDate", required = false) String endDate,
                                  @RequestParam(value = "billType") String billType) {
        return billService.chartBillInYear(loginUser, startDate, endDate, loginUser, billType);
    }

    /**
     * 导出收支明细列表
     */
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/export")
    @ApiException(LOGIN_USER_QUERY_BILL_TOTAL_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @ApiOperation("导出收支明细列表")
    @PermissionCheck(permission = "operation:bill:export")
    public void export(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       @RequestParam(value = "userId", required = false) Integer userId,
                       @RequestParam(value = "billType", required = false) Integer billType,
                       @RequestParam(value = "tradeType", required = false) Integer tradeType,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate,
                       HttpServletResponse response) {
        Bill bill = new Bill();
        bill.setBillType(billType);
        bill.setTradeType(tradeType);
        bill.setUserId(userId);
        billService.exportExcel(loginUser, bill, startDate, endDate, response);
    }

}
